<?php
 include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/global_locale.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/report_php.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_session.inc");
include(DOCINCLUDES."global_files/form_php.inc");
include(DOCINCLUDES."global_files/global_filter_functions.inc");

// Company Details

$customization_id=$_GET["customization_id"]?$_GET["customization_id"]:11;
$date_val=$_GET["date_val"]?$_GET["date_val"]:"ship_date";

$company_id =100351;
$company_name
=($_SESSION["guser_company_name"])?$_SESSION["guser_company_name"]:"Good leather
Pvt Ltd ";


$report_customization=getReportDetails($company_id,$customization_id);

$page = new cPage();
$form = new cForm();
$page->header_path="../../";

$db->query="SELECT report_name,mr.report_id,view_name,title from
$master_report_table mr join $sys_report_customization_table src on
mr.report_id=src.report_id where customization_id=$customization_id ;";
		$db->runQuery();
		$report_detail_array=$db->returnArrays();
		
//Assiging Report Details  to Variables
		$report_id = $report_detail_array [0] ["report_id"] ;
		$report_name = $report_detail_array [0] ["report_name"] ;
		$view_name = $report_detail_array [0] ["view_name"] ;
		$title = $report_detail_array [0] ["title"] ; ;
		
if($_POST)
        {
        $form->validations["from_date"]=array("isEmpty"=>"Please enter from date","isDate"=>"Please enter a valid date");
        $form->validations["to_date"]=array("isEmpty"=>"Please enter to date","isDate"=>"Please enter a valid date");
        $from_date=$_POST["from_date"];
        $to_date=$_POST["to_date"];

//         if(!$form->errors)
//                 {
//                 $period=$_POST["period"];
//                 if($period)
//                         {
//                         list($month_no,$year)=explode("-",$period);
//                         $from_date="01/$month_no/$year";
//                         $time_stamp=mktime(0,0,0,$month_no,1,$year);
//                         $to_date=date("t",$time_stamp)."/$month_no/$year";
//                         }
//                 }

        $group_name=$_POST["group_name"];
        $group_order=$_POST["group_order"];
        $sort_name=$_POST["sort_name"];
        $sort_order=$_POST["sort_order"];
        
	$date_type=$date_val;
	
	if($from_date)	
		{
	$formatted_from_date=($from_date);
	$formatted_to_date=($to_date);
		}
// To Get Constraint Condtion (Filter Condition)
	
	$constraint_condition=generateConstraintCondition($customization_id);

// To Set Date Constraint 	
	
	if ($constraint_condition=="")
		$constraint_condition.=($formatted_from_date[0]!="")?"where $date_type between '$formatted_from_date' and '$formatted_to_date'":"";
	else
		$constraint_condition.=($formatted_from_date[0]!="")?" and $date_type between '$formatted_from_date' and '$formatted_to_date'":"";
}
	
elseif($_GET["action"])
        {
        
        $group_name=$_GET["group_name"];
        $group_order=$_GET["group_order"];
        $sort_name=$_GET["sort_name"];
        $sort_order=$_GET["sort_order"];
	}

if($_GET["action"] != "print_report")
	{
	$reportdate = (($_POST["from_date"]) && ($_POST["to_date"]))? "From $from_date To $to_date ":date("d/m/Y");
	}
else
	{
	$reportdate = $_GET["reportdate"];
	$constraint_condition= $_SESSION["filter"];
	unset($_SESSION["filter"]);
	}

//Report Title with date
$constraint_condition_title=generatetitle($customization_id);
$report_title=($title)?$title:$report_name;
$page->title=$page->heading="$report_title as on ".$reportdate. " ".$constraint_condition_title;

if($_GET["action"] == "print_report")
        {
        $page->doPrintHeader();
        }
else
        {
	$page->doPrintUpperHtml();
        }
if($_GET["action"] != "print_report")
        {
        echo "<div align=\"center\">";
        
	echo
returnLink("../../includes/report_files/filters_customization.php","Customizise
This Report",array("report_code"=>$report_id,"partner_id"=>$company_id,
"customization_id"=>$customization_id));
	echo " | <a  href=\"javascript:Toggle('acustom')\"><b>Available Customizations</b></a>";
        if($_GET["report"] != "summary_report")
                {
                $query_string_summary_report_print=array("customization_id"=>$customization_id,"report"=>"summary_report");
		echo " | ".returnLink($_SERVER["PHP_SELF"],"Summary Report", $query_string_summary_report_print);
                }
	else
                {
		echo "|".returnLink($_SERVER["PHP_SELF"]," View Detailed Report ", array("customization_id"=>$customization_id,"company_id"=>$company_id));
		 }
	
	echo "<div ID=acustom style=\"display:none; margin-left:2em\">";
	
	$db->query="select customization_id,customization_name from $sys_report_customization_table where report_id= $report_id and is_active='t';";
	$db->runQuery();
	$report_customs_array=$db->returnArrays();
	$report_customs_array_count=count($report_customs_array);
	for($i=0;$i<$report_customs_array_count;$i++)
		{ 
			echo " | ".returnLink("$PHP_SELF",$report_customs_array[$i]["customization_name"],array("report_code"=>$report_id,"partner_id"=>$company_id,"customization_id"=>$report_customs_array[$i]["customization_id"]));
		}
	echo "|";
	echo "</div>";

	$constraint_array=getFilters($customization_id,$report_id,$_GET["action"]);
	echo $form->generateInputTag("hidden",array("name"=>"customization_id","value"=>"$customization_id"));
	getGroups($group_name,$group_order,$sort_name,$sort_order,$constraint_array);

	$link="<a
href=\"javascript:show_calendar('report_constraint_selection.from_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"../../images/calendar.gif\" width=\"16\" height=\"16\"
border=\"0\"></a>";
	
	echo "From &nbsp;&nbsp;&nbsp;". $form->generateInputTag("text",array("name"=>"from_date","value"=>"$from_date","size"=>"12","maxlength"=>"10"),"",$link);
	
	$link="<a
href=\"javascript:show_calendar('report_constraint_selection.to_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"../../images/calendar.gif\" width=\"16\" height=\"16\"
border=\"0\"></a>";
	
	echo "To &nbsp;&nbsp;&nbsp;". $form->generateInputTag("text",array("name"=>"to_date","value"=>"$to_date","size"=>"12","maxlength"=>"10"),"",$link);
	
	echo "&nbsp;&nbsp;&nbsp;".$form->generateInputTag("submit",array("accesskey"=>"3","name"=>"show","value"=>"Show Report"))."</form>";
        }

$_SESSION["filter"] = $constraint_condition; 

if(!$_SESSION["cache"])
	{
	$fetch_column=getColumnNames($report_customization["column_mapping"]);
//Fetch Column Names Form Database
	 $db->query="select $fetch_column from _$company_id.$view_name $constraint_condition";
	$db->runQuery();
	$arrays=$db->returnArrays();
	
	$_SESSION["cache"]["page"]=$_SERVER["PHP_SELF"];
	$_SESSION["cache"]["data"]=$arrays;
	}
else
	{
//assign value from the session and use the array to generate reports
	$arrays=$_SESSION["cache"]["data"];
	unset ($_SESSION["cache"]);
	}

 	
if(!$arrays)
      {
        echo "<div align=\"center\">No  Records for $report_name</div><br />";
        $page->doFooterHtml();
        exit;
      }
if($_GET["action"] != "print_report" )
        {
        addNonPrintableColumns($arrays,$company_id);
	}
if($report_customization["summary_columns"]!="")
	{
	
 	$grand_totals_array=getTotal($arrays);
	
	setNA($grand_totals_array);

assignPreferredSummary($grand_totals_array,$report_customization[
"display_summary_columns"]);

assignPreferredColumns($grand_totals_array,$report_customization[
"display_columns"],$report_customization["decimal_places"],
$report_customization["format_details"]);
	}

$group=generateGroupArray($group_name,$group_order);
$sort["$sort_name[0]"]=$sort_order[0];
$pre_sort=($group)?key($group):key($sort);
$pre_sort_order=($group)?current($group):current($sort);

// 	pass the form values in query string

$query_string=array("customization_id"=>$customization_id,"reportdate"=>
$reportdate);
$query_string=array_merge($query_string,generateGroupQueryString($group,$sort));
$query_string_summary_report=array_merge($query_string,array("report"=>
"summary_report"));
$pre_sorted_arrays=sortArray($arrays,$pre_sort,$pre_sort_order);
	
if($_GET["action"] != "print_report" )
{
$ROW_COUNT=300;
}
else
{
$ROW_COUNT=25;
}

if($_GET["report"]=="summary_report" || $place_order)
        {
        $page_arrays=$pre_sorted_arrays;
        }
else
        {
//        
$page_arrays=navigatePage($_GET["page_no"],$pre_sorted_arrays,$ROW_COUNT,
$query_string);
        }
//pass the form values in query string


 if($report_customization["summary_columns"]=="")
showGroupedReport($page_arrays,$group,$sort,$_GET["report"],array(),true,true);
 else
showGroupedReport($page_arrays,$group,$sort,$_GET["report"],"",array(),false);

doHorizontalTable($report_details,"100%","",$report_customization["column_style"
],true,true,false,"report");	

if($_GET["report"]=="summary_report")
        {
        setNA($summary_details);
        assignPreferredColumns($summary_details,$report_customization["display_columns"],$report_customization["decimal_places"],$report_customization["format_details"],"DEFAULT_ORDER",$report_customization["format_date"]);
      doHorizontalTable($summary_details,"100%","",$report_customization["column_style"],true,true,false,"");
        $query_string=$query_string_summary_report;
        }
$grand_size=count($grand_totals_array[0])*10;
doHorizontalTable($grand_totals_array,"$grand_size%","Grand Total",$report_customization["column_style"],true,true,false,"");	

if($_GET["action"] == "print_report" )
        {
        $page->doPrintFooter();
        }
else
        {
     showPrintableReportLink($query_string);
        }
	
?>
<script >
function Toggle(item)
	{
	obj=document.getElementById(item);
	visible=(obj.style.display!="none")
	if (visible)
		{
		obj.style.display="none";
   		}
	 else
		{
      		obj.style.display="block";
		}
	}
</script>
